package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PackingServiceImpl implements PackingService {

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    //分页查询所有装箱数据 *
    public PageInfo findAll(PackingExample packingExample,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Packing> packings = packingDao.selectByExample(packingExample);
        PageInfo pageInfo = new PageInfo(packings);
        return pageInfo;
    }

    //添加装箱单*
    public void save(Packing packing) {
        //获取报运单id
        String exportIds = packing.getExportIds();
        //切割每一个报运单id
        String[] exportIdsArr = exportIds.split(",");
        String exportNos = "";
        String exportIdsStr = "";
        for (String exportId : exportIdsArr) {
            //遍历报运单id获取每个报运单下信用证号并拼接
            Export export = exportDao.selectByPrimaryKey(exportId);
            exportNos+=export.getLcno()+" ";
            exportIdsStr+=export.getId()+" ";
            export.setState(3);
            exportDao.updateByPrimaryKeySelective(export);
        }
        packing.setExportNos(exportNos);
        packing.setExportIds(exportIdsStr);
        //设置装箱单号
        packing.setPackingListId(UUID.randomUUID().toString().replace("-",""));
        //设置发票单号
        packing.setInvoiceNo(UUID.randomUUID().toString().replace("-",""));
        //设置发票单产生的时间
        packing.setInvoiceDate(new Date());
        //设置装箱状态
        packing.setState(0l);
        //设置创建时间
        packing.setCreateTime(new Date());
        packingDao.insertSelective(packing);

    }

    //修改装箱单*
    public void update(Packing packing) {
        packingDao.updateByPrimaryKeySelective(packing);
    }

    //查询装箱单详情*
    public Packing findById(String id) {
        return packingDao.selectByPrimaryKey(id);
    }

    //删除装箱单*
    public void delete(String id) {
        packingDao.deleteByPrimaryKey(id);
    }

    /*@Override
    public List<Packing> findAllPacking(String companyId) {
        packingDao.findAllPacking(companyId);
        return null;
    }*/
}
